<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开启支付">
          <a-switch checked-children="启用" un-checked-children="禁用" default-checked @change="switchOn"
                    v-model="checked"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开启提现">
          <a-switch checked-children="启用" un-checked-children="禁用" default-checked @change="switchOn2"
                    v-model="checked2"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户-直推佣金">
          <a-input-number
            v-decorator="[ 'userDirectPushCommission', {rules: [{ required: true, message: '请输入用户佣金分成!' }]}]"
            :min="0" :max="100"/>
          %
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户-间推佣金">
          <a-input-number
            v-decorator="[ 'userIndirectCommission', {rules: [{ required: true, message: '请输入用户佣金分成!' }]}]"
            :min="0" :max="100"/>
          %
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="陪诊师-直推佣金">
          <a-input-number
            v-decorator="[ 'escortDirectPushCommission', {rules: [{ required: true, message: '请输入陪诊师佣金分成!' }]}]"
            :min="0" :max="100"/>
          %
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="陪诊师-间推佣金">
          <a-input-number
            v-decorator="[ 'escortIndirectCommission', {rules: [{ required: true, message: '请输入陪诊师佣金分成!' }]}]"
            :min="0" :max="100"/>
          %
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单过期时长">
          <a-input-number v-decorator="[ 'orderExpirationTime', {rules: [{ required: true, message: '请输入订单过期时长!' }]}]"
                          :min="1"/>
          分钟
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="首页电话">
          <a-input v-decorator="[ 'homePhone', {rules: [{ required: true, message: '请输入首页电话!' }]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分享logo">
          <j-upload text="分享logo"
                    :number="1"
                    v-decorator="['shareLogo', {rules: [{ required: true, message: '请选择分享logo!', }]}]"
                    fileType="image"></j-upload>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分享内容">
          <a-input v-decorator="[ 'shareContent', {rules: [{ required: true, message: '请输入分享内容!' }]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="充值比例">
          <a-input-number v-decorator="[ 'rechargeRatio', {rules: [{ required: true, message: '请输入充值比例!' }]}]"/>
          <p style="color: red;">备注：输入比例积分即可，例如：1(元)：100(分)，输入100即可</p>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import JUpload from '@/components/jeecg/JUpload'

  export default {
    name: "UserPayConfigModal",
    components: {
      JUpload,
    },
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        checked: true,
        checked2: true,
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "/user/userPayConfig/add",
          edit: "/user/userPayConfig/edit",
        },
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.checked = true;
        this.checked2 = true;
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        console.info(record);
        if (record.id) {
          this.checked = record.isStartPayment == 1 ? true : false;
          this.checked2 = record.isStartWithdraw == 1 ? true : false;
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'shareLogo','shareContent','orderExpirationTime', 'homePhone', 'userIndirectCommission', 'escortIndirectCommission','escortDirectPushCommission', 'userDirectPushCommission', 'isStartPayment', 'isStartWithdraw', 'rechargeRatio', 'delFlag'))
          //时间格式化
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            console.log(formData);
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      switchOn(checked) {
        if (checked) {
          this.model.isStartPayment = 1;
          this.checked = true;
        } else {
          this.model.isStartPayment = 0;
          this.checked = false;
        }
      },
      switchOn2(checked2) {
        if (checked2) {
          this.model.isStartWithdraw = 1;
          this.checked2 = true;
        } else {
          this.model.isStartWithdraw = 0;
          this.checked2 = false;
        }
      },
    }
  }
</script>

<style lang="less" scoped>

</style>